package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_Anchor_Adapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Discover_AnchorService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_AnchorStytle;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_AnchorTotle;
import com.qiaoyanqing.ximalaya.url.Urlconfig;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_AnchorFragment extends Fragment implements AbsListView.OnScrollListener, Callback<Discover_AnchorTotle> {


    public int PAGE = 1;
    private boolean isScrollToBottom = true;
    private View inflate;
    private ListView listView;
    private List<Discover_AnchorStytle> mDiscoverAnchorStytles = new ArrayList<>();
    private Discover_Anchor_Adapter adapter;

    public Discover_AnchorFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_anchor, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.ANCHOR_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Discover_AnchorService anchor = retrofit.create(Discover_AnchorService.class);
        anchor.style(PAGE).enqueue(this);
    }


    private void initView(View inflate) {
        listView = ((ListView) inflate.findViewById(R.id.list_view));
        adapter = new Discover_Anchor_Adapter(getContext(), mDiscoverAnchorStytles);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (isScrollToBottom && i == SCROLL_STATE_IDLE) {
            PAGE++;
            if (PAGE < 4) {
                initData();

            }
        }

    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if (i + i1 == i2) {
            isScrollToBottom = true;

        } else {
            isScrollToBottom = false;
        }
    }


    @Override
    public void onResponse(Call<Discover_AnchorTotle> call, Response<Discover_AnchorTotle> response) {
        if (response.body().getList().size() > 0) {
            mDiscoverAnchorStytles.addAll(response.body().getList());
            for (int i = 0; i < mDiscoverAnchorStytles.size(); i++) {
                if (mDiscoverAnchorStytles.get(i).getList()==null) {
                    mDiscoverAnchorStytles.remove(i);
                }
            }
            Log.d("TAG", response.body().getList().toString());
            adapter.notifyDataSetChanged();

        }
    }

    @Override
    public void onFailure(Call<Discover_AnchorTotle> call, Throwable t) {

    }
}
